aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/invoice/[id]/+layout.server.js
blob: dacc44e627c4896055d33c3e1abcc582414c66e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import Qr from 'qrcode-base64';
import { get } from '$lib/utils';
import { redirect } from '@sveltejs/kit';

export async function load({ depends, params, url }) {
	depends('app:invoice');

	let { id } = params;
	let invoice = await get(`/invoice?uuid=${id}`);
	let { amount, received, pending } = invoice;

	let paid = (!amount && (received || pending)) || (amount > 0 && received + pending >= amount);
	if (paid && !url.pathname.endsWith('paid')) throw redirect(307, `/invoice/${id}/paid`);

	let sm = Qr.drawImg(invoice.text, { size: 300 });
	let lg = Qr.drawImg(invoice.text, { size: 1200 });

	return { id, invoice, sm, lg };
}